﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.IO;
using System.Drawing;

using Castle.Core.Logging;

using InfoGenerateSystem.Persistence.IDao;
using InfoGenerateSystem.Core.Utils;
using InfoGenerateSystem.Domain;

namespace InfoGenerateSystem.WebView
{
    /// <summary>
    /// 图片查看测试页面
    /// 
    /// 创建人:     王昊
    /// 创建时间:   2009-06-16
    /// 
    /// </summary>
    public partial class PicViewer : System.Web.UI.Page
    {
        private IGenericDao _dao = ServiceHelper.GetService(typeof(IGenericDao)) as IGenericDao;
        private ILogger _log = ServiceHelper.GetService(typeof(ILogger)) as ILogger;

        private Int64 labelID;
        private Int64 LabelID
        {
            get
            {
                labelID = Convert.ToInt64(Request.QueryString["labelID"]);
                return labelID;
            }
        }

        private Int64 accountCode;
        public Int64 AccountCode
        {
            get
            {
                if (Request.QueryString["accountCode"] != null)
                {
                    accountCode = Convert.ToInt64(Request.QueryString["accountCode"]);
                }
                else
                    accountCode = 0;
                return accountCode;
            }
            set { accountCode = value; }
        }

        private String stkCode;

        public String StkCode
        {
            get
            {
                stkCode = Request.QueryString["stkCode"];
                return stkCode;
            }
            set { stkCode = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Response.Clear();
                TblbStockIndexesGraphyData sgd = new TblbStockIndexesGraphyData();
                TblbAccountGraphyData agd = new TblbAccountGraphyData();
                sgd.LABELID = LabelID;
                agd.LABELID = LabelID;
                if (!String.IsNullOrEmpty(StkCode))
                {
                    sgd.STOCKCODE = StkCode;
                    TblbStockIndexesGraphyData acc = _dao.Find("TblbStockIndexesGraphyData.FindByLabelIdAndStkCode", sgd) as TblbStockIndexesGraphyData;
                    ShowImage(acc.NR, acc.FILENAME, "jpg");
                }
                else if (AccountCode != 0)
                {
                    agd.ACCOUNTCODE = AccountCode;
                    TblbAccountGraphyData acc = _dao.Find("TblbAccountGraphyData.FindByLabelIdAndAccoutCode", agd) as TblbAccountGraphyData;
                    ShowImage(acc.NR, acc.FILENAME, "jpg");
                }
            }
            catch (Exception)
            {
                Response.Write("数据库中未找到此条附件或连接失败");
            }
        }

        public void ShowImage(Byte[] b, String fileName, String extName)
        {
            if (b.Length > 0)
            {
                MemoryStream stream = new MemoryStream(b, true);
                stream.Write(b, 0, b.Length);
                Bitmap bmp = new Bitmap(stream);

                Response.ContentType = "image/jpeg";
                bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

                Response.End();

                stream.Close();
            }

            //String filename = HandleFileName(fileName);

            //try
            //{
            //    //string mm = _ms.GetMimeByFilename(filename);
            //    Response.Clear();
            //    Response.ContentType = extName; //mm
            //    //文件名,中文名要处理

            //    Response.AppendHeader("Content-Disposition",
            //            string.Format("attachment;filename={0}", filename + extName));
            //    byte[] buf = b;
            //    Response.OutputStream.Write(buf, 0, buf.Length);
            //}
            //catch (Exception ex)
            //{
            //    _log.Error("FileDownload error:" + ex.Message);
            //}
        }
        private string HandleFileName(string s)
        {
            if (s.Length > 19)
            {
                s = s.Substring(0, 16) + System.IO.Path.GetExtension(s);
            }
            s = HttpUtility.UrlPathEncode(s);
            return s;
        }
    }
}
